/*
hdPic:高清组图专用脚本
@版本:hd2011_v2.7.2
@作者:tomiezhang#tencent.com
@时间:2011-3-18
*/
	var hdPic = window.hdPic = function(p){
		return hdPic.fn.init(p);
	};
	hdPic.fn=hdPic.prototype = {
			_tmpArray:[],
			_lastUrl:"",
			_lastTitle:"",
			_isgoOn:false,
			_coentArray:"",
			_coreurl:"http://mat1.gtimg.com/www/core/core.js",
			_pageNow:0,
			_isMove:false,
			_dragx:0,
			_isAuto:false,
			_autoTimer:null,
			_nowSrc:new Image(),
			_preloadN:new Image(),
			_preloadP:new Image(),
			_sourName:"",
			_sourUrl:"",
			_pubTime:"",
			_siteName:"",
			_siteLink:"",
			_isPic:true,
			_isCiment:false,
			_aid:0,
		    _siteEname:"news",
			_auth:"",
			_maxHeight:433,
			_coreload:function(file,callback) {
			try {
				var script = document.createElement('script');
				script.src = file;
				script.type = "text/javascript";
				document.getElementsByTagName("head")[0].appendChild(script);
				if( script.addEventListener ) {
					script.addEventListener("load", callback, false);
				} else if(script.attachEvent) {
					script.attachEvent("onreadystatechange", function(){
							if(script.readyState == 4
								|| script.readyState == 'complete'
								|| script.readyState == 'loaded') {
								callback();
							}
					});
				}
			} catch(e) {
				callback();
			}
		},
		_getReady:function(){//大图首次载入ready，初始化播放器区域高度，图片切换效果、hover效果
					$(".pageLeft-bg").show();
					$(".pageRight-bg").show();
					$(".pageLeft").height($("#Main-A").height());
					$(".pageLeft .left_top").css("margin-top",parseInt(($("#Main-A").height()-95)/2));
					$(".pageLeft-bg").height($("#Main-A").height());
					$(".pageRight").height($("#Main-A").height());
					$(".pageRight .right_top").css("margin-top",parseInt(($("#Main-A").height()-95)/2));
					$(".pageRight-bg").height($("#Main-A").height());
					$("#mouseOverleft").height($("#Main-A").height());
					$("#mouseOverleft").width(parseInt($("#Main-A").width()/2));
					$("#mouseOverright").height($("#Main-A").height());
					$("#mouseOverright").width(parseInt($("#Main-A").width()/2));
					$("#mouseOverleft").hover(function(){
						$(".pageLeft").fadeIn("fast");
					},function(){
						$(".pageLeft").fadeOut("fast");
					});
					$("#mouseOverright").hover(function(){
						$(".pageRight").fadeIn("fast");
					},function(){
						$(".pageRight").fadeOut("fast");
					});
				$("#Main-C").fadeTo('slow',1).fadeTo('slow',0.2).fadeTo('slow',1);
		},
		_clickleft:function(data){//向前点
			if(hdPic.fn._pageNow>0){
						hdPic.fn._pageNow--;
					}else{
						hdPic.fn._pageNow = 0;
					}
				hdPic.fn._showBig(data,hdPic.fn._pageNow);
		},
		_clickright:function(data){//向后点
			if(hdPic.fn._pageNow<data.length-1){
						hdPic.fn._pageNow++;
						hdPic.fn._showBig(data,hdPic.fn._pageNow);
			 }else{
					hdPic.fn._pageNow =0;
					hdPic.fn._showBig(data,hdPic.fn._pageNow);
			 }
		},
		_bindClick:function(data){//为各种按钮绑定事件、拖拽浏览、快捷键、页面初始焦点
			$("#Smailllist li").each(function(i){
				$(this).click(function(){
					/*hdPic.fn._stopAuto();*/
					hdPic.fn._showBig(data,i);
				})
			});
			$("#Smailllist img").each(function(i){
				$(this).hover(
					function(){
						$(this).fadeTo("fast",1);
					},
					function(){
						$(this).fadeTo("fast",0.6);
					}
				);
			});
			$("#mouseOverright").bind('click',function(){
				hdPic.fn._stopAuto();
				hdPic.fn._clickright(data);
			});
			$("#goright").bind('click',function(){
				hdPic.fn._stopAuto();
				hdPic.fn._clickright(data);
			});
			$("#mouseOverleft").bind('click',function(){
				hdPic.fn._stopAuto();
				hdPic.fn._clickleft(data);
			});
			$("#goleft").bind('click',function(){
				hdPic.fn._stopAuto();
				hdPic.fn._clickleft(data);	
			});
			//拖拽浏览
			if(hdPic.fn._tmpArray.length>5){
				$(".scrollButton").bind("selectstart",function(){return false;})
				$(".scrollButton").click(function(){}).mousedown(function(e){
						hdPic.fn._stopAuto();
					  //设置捕获范围
					  if($(".scrollButton").setCapture){
						  $(".scrollButton").setCapture();
					  }else if(window.captureEvents){
						  window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
					  }
					hdPic.fn._isMove = true;
					hdPic.fn._dragx = e.pageX-parseInt($(".scrollButton").css("left"));
					$(".scrollButton").fadeTo(20, 0.5);
					$("a.mask").hide();
				});
				 $(document).mousemove(function(e){
					if(hdPic.fn._isMove){
						var x=Math.max(0, Math.min(e.pageX-hdPic.fn._dragx,560));
						 $(".scrollButton").css({left:x});
						hdPic.fn._dragmov();
					}
				 }).mouseup(function(){
					hdPic.fn._isMove=false;
					//取消捕获范围
					 if($(".scrollButton").releaseCapture){
						  $(".scrollButton").releaseCapture();
					   }else if(window.captureEvents){
						  window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
					  }
					$(".scrollButton").fadeTo("fast", 1);
					if(parseInt($("#Smailllist").css("left"))%91!==0){
						var argleft = parseInt($("#Smailllist").css("left"));
						$("#Smailllist").animate({left:argleft+(Math.abs(parseInt($("#Smailllist").css("left"))%91))+"px"},"fast");
					};
				 })
			};
			//自动播放
			/*
			$(".play").click(function(){
				if(!hdPic.fn._isAuto){
					hdPic.fn._autoplay(data);
				}else{
					hdPic.fn._stopAuto();
				}
			});*/
			//快捷键
			$(document).bind("keydown",function(e){
				e = window.event || e;
				hdPic.fn._stopAuto();
				e.keyCode == 37 && hdPic.fn._clickleft(data);
				e.keyCode == 39 && hdPic.fn._clickright(data);
				e.keyCode == 38 && hdPic.fn._clickleft(data);
				e.keyCode == 40 && hdPic.fn._clickright(data);
			});
			//设置焦点
			/*
				 var scrollPos; 
				if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') { 
					scrollPos = document.documentElement; 
				} 
				else if (typeof document.body != 'undefined') { 
					scrollPos = document.body; 
				} 
				var _topnav = $("#toolBar").offset().top;
				$(scrollPos).animate({scrollTop:_topnav - 10}, 1000);
			*/
		},
		_stopAuto:function(){//停止自动播放
			$(".play").html("幻灯播放");
			$(".play").removeClass("stop");
			hdPic.fn._isAuto = false;
			window.clearInterval(hdPic.fn._autoTimer);
		},
		_autoplay:function(data){//自动播放
			$(".play").html("停止播放");
			$(".play").addClass("stop");
			hdPic.fn._isAuto = true;
				this._autoTimer = window.setInterval(function(){
					if(hdPic.fn._pageNow<data.length-1){
						hdPic.fn._pageNow++;
						hdPic.fn._showBig(data,hdPic.fn._pageNow);
					}else{
						hdPic.fn._stopAuto();
						hdPic.fn._getLast(data);
					}
					
				},5000)
		},
		_dragmov:function(){//拖拽浏览用函数
			 var a = parseInt(91 * (parseInt($(".scrollButton").css("left"))/610)*(this._tmpArray.length -5));
			 $("#Smailllist").css({left:-a+"px"});
		},
		_replaceTitle:function(){//修正标题bug
			var title = document.title.replace(/#p.\d/i,"");
			document.title = title;
		},
		_showtit:function(n){//设置图片图注显示
			var sours = hdPic.fn._sourUrl!==""?"<a href='"+hdPic.fn._sourUrl+"' target='_blank'>"+hdPic.fn._sourName+"</a>":"<span>"+hdPic.fn._sourName+"</span>";
			var zuozhe = hdPic.fn._auth!==""?"<span style='padding-left:13px;padding-right:0px;'>"+hdPic.fn._auth+"</span>":"";
			if(this._tmpArray[n].showtit==""){
				$("#Main-B").html('<div class="TimeInfo"><span>'+hdPic.fn._pubTime+'</span>'+sours+''+zuozhe+'</div>');//图注装载
			}else{
				$("#Main-B").html('<P>'+this._tmpArray[n].showtit+'</p><div class="TimeInfo"><span>'+hdPic.fn._pubTime+'</span>'+sours+''+zuozhe+'</div>');//图注装载
			}
			$("#Main-B").fadeIn("slow");
			loadingProcess.setPicHandler(n);
		},
		_creatUrl:function(n){//创建组图浏览url标识
			var _org = /\#p\=/i.test(window.location.href);
			if(!_org){
				window.location.href = window.location.href+"#p=1";
			}else{
				window.location.href = window.location.href.split("#p=")[0] + "#p="+parseInt(n+1);
			}
		},
		_getUrl:function(){//获得组图url标识
			var str = window.location.href.toString(),pos = str.indexOf("#p=");
			var nub = 1;
			if(pos!==-1){
					nub=str.match(/\#p\=(\d{1,})/i)[1];
				}
			return nub;
		},
		_Pload:function(data,n){//预加载前后
			if(data.length>3){//大于3张 才预加载
				if (n != Number(data.length - 1)) {
					this._preloadN.src = data[n + 1].bigpic
				}
			}
		},
		_showBig:function(data,n){//显示大图、显示成功后设置索引值对应的图注、url、组图当前索引值改写、小图位置、统计
			indexPic = n;
			$("#PicSrc").attr("src","http://mat1.gtimg.com/news/hd2009fin/ajax-loader.gif");
			hdPic.fn._Pload(data,n);
			$("#PicSrc").load(function(){
				hdPic.fn._autoSca($(this),data[n].bigpic);
				hdPic.fn._replaceTitle();
				$(this).height()>hdPic.fn._maxHeight?$("#Main-A").height($(this).height()):$("#Main-A").height(hdPic.fn._maxHeight);
				$(this).css("margin-top",parseInt($("#Main-A").height()-$(this).height())/2+"px");
			});
			$("#PicSrc").fadeTo("fast", 0, function(){
					hdPic.fn._pageNow = n;
					hdPic.fn._showSmall(n);
					hdPic.fn._creatUrl(n);
					$("#PicSrc").attr("src",data[n].bigpic);
					$("#PicSrc").fadeTo("fast",1,function(){
						document.getElementById("PicSrc").style.filter = "";
					});
			 });
		},
		_autoSca:function($this,src){
			var img = new Image();
			img.src = src;
			/*
			if (img.width > 0 && img.height > 0) {//都大于0
				if (img.width / img.height >= hdPic.fn._maxHeight / 600){
						if(img.width>600) {
                            $this.width(600);
                        }
                      
				}else{

				}
			}*/
		},
		_showSmall:function(n){//小图移动切换逻辑
			$("#Smailllist img").each(function(i){
				$(this).fadeTo('fast',0.6);
			});
			$("#Smailllist img:eq("+n+")").fadeTo('fast',1);
			$("a.mask").show();
			if(hdPic.fn._tmpArray.length<=5){
				$("a.mask").animate({left: (91*n)+"px"},"fast");
				return false;
			}
			var _left,_latsindex=5;
			if(n>=2 && n<(hdPic.fn._tmpArray.length)-2){//大于2小于倒数2
				$("#Smailllist").animate({left:-91*(n-2)+"px"},"slow",function(){
						_left = (91*2)+"px";
						$("a.mask").animate({left:_left},"fast");
						$(".scrollButton").animate({left:_left},"fast");
				});
			}else{
				if(n>=(hdPic.fn._tmpArray.length)-2){
					_left = (91*(_latsindex-(hdPic.fn._tmpArray.length-n)))+"px";
					$("#Smailllist").animate({left:-(hdPic.fn._tmpArray.length-5)*91+"px"},"fast");
					$("a.mask").animate({left:_left},"fast");
					$(".scrollButton").animate({left:_left},"fast");
				}else{
					if(n<3){
						$("#Smailllist").animate({left:"0px"},"fast");
					}
						_left = (91*n)+"px";
						$("a.mask").animate({left:_left},"fast");
					
					$(".scrollButton").animate({left:_left},"fast");
				}
				
			}
			
		},
		_getData:function(data){//第一次加载后，初始化大图、小图、绑定事件
			if(data.length>0){
				/*成功*/
				$("#Main-A").append("<img src="+data[parseInt(hdPic.fn._getUrl()-1)].bigpic+" id='PicSrc' style='display:none'/>");
				this._getReady();//大图ready
				this._small(data);//装载小图
				this._pageNow = parseInt(hdPic.fn._getUrl()-1);
				this._bindClick(data);
				this._showBig(data,parseInt(hdPic.fn._getUrl()-1));
			}
		},
		_small:function(data){//第一次加载后初始化小图
			var _tmp="",ulLength=131*data.length;
			$.each(data,function(i){
					if(i==0){
					_tmp+='<li><div><a href="javascript:void(0);" class="select"  onfocus="this.blur()"><img src="'+data[i].smallpic+'"/></a></div></li>';
					}else{
					_tmp+='<li><div><a href="javascript:void(0);"  onfocus="this.blur()"><img src="'+data[i].smallpic+'"/></a></div></li>';
					}
			});
			$("#Smailllist").width(ulLength);
			$("#Smailllist").html(_tmp);
			$("#Smailllist img").each(function(i){
				$(this).fadeTo('fast',0.6);
			});
		},
		_query:function(){//第一次加载,使用ajax加载数据,并在成功后把数据格式化到本地,url自行拼装
			 var org = window.location.href;
			$.ajax({  
              url: org.split(".htm")[0]+".hdBigPic.js?time="+Math.random(), 
               type: "GET",   
               beforeSend: function(x) {  
				   x.setRequestHeader("If-Modified-Since","0");
                   x.setRequestHeader("Charset", "UTF-8");
				   x.setRequestHeader("Cache-Control","no-cache");
                },  
               success: function() { //ajax成功,将数据载入本地
                   var arrMe = eval("(" + arguments[0] + ")");
					var length = arrMe.Children[0].Children[0].Children[0].Content;//长度
					for(var i=0;i<length;i++){
						var txt = arrMe.Children[0].Children[1].Children[i].Children[3].Children[0].Content;
						hdPic.fn._tmpArray.push({'showtit':''+txt.replace(/\<p\>/i,"").replace(/\<\/p\>/i,"")+'', 'showtxt':''+arrMe.Children[0].Children[1].Children[i].Children[0].Children[0].Content+'', 'smallpic':''+arrMe.Children[0].Children[1].Children[i].Children[1].Children[0].Content+'', 'bigpic':''+arrMe.Children[0].Children[1].Children[i].Children[2].Children[0].Content+''})
					}
					hdPic.fn._getData(hdPic.fn._tmpArray);//执行数据
               },  
               error: function(x, e) {  
					$("#Pic-Mod-wrap").html("<div style='margin:30px auto'>数据加载错误!</div>");
               },  
               complete: function(x) {  
                 //  alert(x.responseText);  
               }  
           });  
		},
		_test:function(p){//测试接口，直接传值进来
			if(p){
				hdPic.fn._tmpArray = p;
				hdPic.fn._getData(hdPic.fn._tmpArray);//执行数据
			}
		},
		init:function(p){
			this._coreload(this._coreurl,function(){
					$(document).ready(function($){
						//hdPic.fn._query();
						hdPic.fn._test(p);
					});
			});
		}
	}
	hdPic.fn.init.prototype = hdPic.fn;